package com.yxt.qmgf.klotski.service;

import com.yxt.qmgf.klotski.vo.GameConfig;
import com.yxt.qmgf.klotski.vo.GameState;

import java.util.Random;

public class GameService {
    private GameState currentState;
    private final GameConfig config;

    public GameService(GameConfig config) {
        this.config = config;
        initializeGame();
    }

    private void initializeGame() {
        int size = config.getSize();
        int[][] board = new int[size][size];

        // 初始化有序棋盘
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                board[i][j] = i * size + j + 1;
            }
        }

        // 设置空白格(最后一个格子为0表示空白)
        board[size-1][size-1] = 0;
        currentState = new GameState(board, size-1, size-1, 0);

        // 随机打乱棋盘
        shuffleBoard();
    }

    private void shuffleBoard() {
        Random random = new Random();
        int size = config.getSize();
        int shuffleCount = size * size * 20; // 打乱次数

        for (int i = 0; i < shuffleCount; i++) {
            int direction = random.nextInt(4);
            int newEmptyRow = currentState.getEmptyRow();
            int newEmptyCol = currentState.getEmptyCol();

            switch (direction) {
                case 0: // 上
                    newEmptyRow--;
                    break;
                case 1: // 下
                    newEmptyRow++;
                    break;
                case 2: // 左
                    newEmptyCol--;
                    break;
                case 3: // 右
                    newEmptyCol++;
                    break;
            }

            if (isValidMove(newEmptyRow, newEmptyCol)) {
                moveTile(newEmptyRow, newEmptyCol);
            }
        }
        currentState.setMoveCount(0); // 重置移动计数
    }

    public boolean isValidMove(int row, int col) {
        int size = config.getSize();
        if (row < 0 || row >= size || col < 0 || col >= size) {
            return false;
        }

        int emptyRow = currentState.getEmptyRow();
        int emptyCol = currentState.getEmptyCol();

        // 只能移动与空白格相邻的格子
        return (Math.abs(row - emptyRow) == 1 && col == emptyCol) ||
                (Math.abs(col - emptyCol) == 1 && row == emptyRow);
    }

    public void moveTile(int row, int col) {
        if (!isValidMove(row, col)) return;

        int[][] board = currentState.getBoard();
        int emptyRow = currentState.getEmptyRow();
        int emptyCol = currentState.getEmptyCol();

        // 交换目标格子和空白格
        board[emptyRow][emptyCol] = board[row][col];
        board[row][col] = 0;

        // 更新空白格位置
        currentState.setEmptyRow(row);
        currentState.setEmptyCol(col);
        currentState.setMoveCount(currentState.getMoveCount() + 1);
    }

    public boolean isGameSolved() {
        int size = config.getSize();
        int[][] board = currentState.getBoard();

        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                int expectedValue = (i == size-1 && j == size-1) ? 0 : i * size + j + 1;
                if (board[i][j] != expectedValue) {
                    return false;
                }
            }
        }
        return true;
    }

    public GameState getCurrentState() {
        return currentState;
    }
}
